import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/house/house_list.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';

import 'pages/404.dart';
import 'pages/house/location_list.dart';
import 'pages/login/index.dart';
import 'pages/notice/index.dart';
import 'pages/profile/index.dart';
import 'utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      "/": (context) => const TabsPage(),
      "/notice_detail": (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    // 默认路由 第一个页面访问哪一个 ability中 loadContent()
    initialRoute: "/",
    // 路由拦截
    // setting 路由对象  setting.name 路由名称  setting.arguments 路由参数
    onGenerateRoute: (setting) {
      // 获取token 判断是否登录
      final token = tokenManager.getToken();
      if (token.isEmpty) {
        // token不存在 跳转到登录页面
        return MaterialPageRoute(builder: (context) => const LoginPage());
      } else {
        if (setting.name == '/profile') {
          // 放行
          return MaterialPageRoute(
              builder: (context) => ProfilePage(
                    userInfo: setting.arguments as Map<String, dynamic>,
                  ));
        }
        if (setting.name == '/house') {
          return MaterialPageRoute(builder: (context) => const HouseList());
        }
        if (setting.name == '/add_house') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        }
      }
      // 404 页面
      return MaterialPageRoute(builder: (context) => const NotFound());
    },
  ));
}
